package type;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Hashtable;
import java.util.Vector;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.FileUtility;
import file.FileWriter;
import utility.Input;


public class Receiver 
{	
	//SearchRecord sr;
	RecordSearch ps;
	String path;
	String RECEIVER="receiver";
	GetIdName gid;
	DBManager db;
	String EMAILID="Emailid";
	int index_pv=-1;
	int index_vt=-1;
	String SendMode;
	String transcation_type;
	String TRASACTION_TYPE="transaction_type";
	
    public Receiver(String path,String transcation_type,DBManager db,String SendMode) 
     {
    	 this.path=path;
    	 this.db =db; 
    	 gid =new  GetIdName(db);
    	 RECEIVER = gid.getId(RECEIVER);
    	 TRASACTION_TYPE = gid.getId(TRASACTION_TYPE);
    	 this.SendMode=SendMode;
    	 this.SendMode = gid.getId(SendMode);
    	 this.transcation_type =transcation_type;
     }
     
     public Vector<Object> findReceiver(String linet)
 	 {
    	try 
    	{
    		Vector<Object> RECEIVERnumber = new Vector<Object>();
			//RandomAccessFile raf =new RandomAccessFile(path,"r");  
			//FileUtility fu =new FileUtility(raf);
//			index_vt = fu.getColumnIndex("vt")-1;
//			index_pv = fu.getColumnIndex("pv")-1;
    		index_vt = Column.vt_index-1;
			index_pv = Column.pv_index-1;
			
			Hashtable<Object, Object>htable = new Hashtable<Object, Object>();
			htable.put("pid", RECEIVER);
			/*sr = new SearchRecord(path);
			sr.fileOpen();
			String line[]  = sr.getRecordSet(htable);
			sr.fileClose();*/
			htable.put("td","null");
			ps = new RecordSearch(db);
			ps.setConditionMap(htable);
			String line[] = ps.getArrayFromResultSet();
			for(int i=0;i<line.length;i++)
			{
				String tempNumber;
				String [] Split = line[i].split("\t");
				if(Split[index_pv].equals("0"))
					tempNumber = getRECEIVER(Split[index_vt],linet);
				else
					tempNumber = getRECEIVER(gid.getItem(Split[index_pv]),linet);
				if(RECEIVERnumber.contains(tempNumber)==false)
					RECEIVERnumber.add(tempNumber);
			}
			//raf.close();
			System.out.println("The mobile number :  " +RECEIVERnumber);
			return RECEIVERnumber;
		} 
    	catch (Exception e) 
    	{
		}
    	return null;
 	}
     
     
     public String getRECEIVER(String reciver,String eventObject)
 		{
    	 
    	 try 
 		{
 			String sub_reciver = reciver.substring(1);
 			long mobile_number = Long.parseLong(sub_reciver);
 			System.out.println("Mobile No is:"+mobile_number);
 			return reciver;
 		}
 		catch (NumberFormatException e) 
 		{
 			try 
 			{
				if(reciver.contains("@")) 
					return reciver;
				String tran_path = Input.TRASACTIONREPORTSPATH + TRASACTION_TYPE +"//"+ gid.getId(transcation_type) + Input.FILRFORMAT;
				RandomAccessFile raf = new RandomAccessFile(tran_path,"r");
				FileUtility fu = new  FileUtility(raf);
				int index = fu.getColumnIndex(reciver)-1;
				raf.close();
				String name[] = eventObject.split("\t"); 
				//String path = Input.MASTERREPORTSPATH + name[index] + "//" + name[index] + "_mid" + Input.FILRFORMAT; 
				Hashtable<Object, Object> table=  new Hashtable<Object, Object>();
				table.put("pid",SendMode);
				table.put("mid", name[index]);
				/*sr = new SearchRecord(path);
				sr.fileOpen();
				String line[] = sr.getRecordSet(table);
				sr.fileClose();*/
				table.put("td","null");
				ps = new RecordSearch(db);
				ps.setConditionMap(table);
				String line[] = ps.getArrayFromResultSet();
				for(int i=0;i<line.length;i++)
				{	
					String Split[]=line[i].split("\t");
					if(Split[index_pv].equals("0"))
						return Split[index_vt];
					else
						return gid.getItem(Split[index_pv]);
				}
			}
 			catch (Exception e1) 
 			{
				e1.printStackTrace();
			}
 		
 		} 
 		return null;
 	}
    public static void main(String[] args) 
    {
    	DBManager db = new DBManager();
    	db.getConnect();
    	String eventobject="5616";
    	String path=Input.MASTERREPORTSPATH + eventobject + "//" +eventobject +"_mid" + Input.FILRFORMAT;;
    	Receiver re = new Receiver(path,"68",db,"Mobileno");
    	System.out.print(re.findReceiver("5634	2007-04-16 14:04:38	68	0	3970	0	26"));
	} 
}
